import sys
import urllib2
import datetime
import lxml.html
import cookielib
import httplib
import re
import codecs
from time import sleep

PAGE_ENCODING='cp1252'


timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H%M%S')
M3U_LIST_FILE_PATH = u'urls-{0:s}.m3u'.format(timestamp)

HTML_LIST_FILE_PATH = u'urls-{0:s}.html'.format(timestamp)

httplib.HTTPConnection.debuglevel = 1

DEFAULT_SLEEP_TIME = 5
sleeptime = (len(sys.argv) > 1) and float(sys.argv[1]) or DEFAULT_SLEEP_TIME

cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
opener.addheaders = [('User-agent',
                      'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1018.0 Safari/535.19'),
                     ('Referer', 'http://translate.google.com/')
]
urllib2.install_opener(opener)

html_data = opener.open(START_PAGE_URL).read().decode(PAGE_ENCODING)

doc = lxml.html.fromstring(html_data)

links = doc.xpath('//a[@onclick]')

popup_links = []

def extract_video_url_from_page(page_url, xpath, parameter_name):
    try:
        html_data = opener.open(page_url).read().decode(PAGE_ENCODING)
        lxml_doc = lxml.html.fromstring(html_data)
        nodeParameter = lxml_doc.xpath(xpath)

        if len(nodeParameter):
            sopUrl =nodeParameter[0].get(parameter_name)
            return sopUrl
        else:
            print "Error! Could not extract param %s from page %s with xpath %s" % (parameter_name, page_url, xpath)
    except Exception as exc:
        print exc

def write_link(file_handle, href, text):
    file_handle.write(u'<a href="%s">%s</a>\n' % (href, text))

with codecs.open(HTML_LIST_FILE_PATH, 'w', 'utf-8') as f:
    f.write(u'''<html>
    <head>
        <style>
            a {
             display: block;
             line-height:70%;
             size: 120%;
             padding: 1em;
            -moz-box-shadow: 0 0 8px 3px #E0E0E0;/*FF 3.5+*/
            -webkit-box-shadow: 0 0 8px 3px #E0E0E0;/*Saf3-4, Chrome, iOS 4.0.2-4.2, Android 2.3+*/
            box-shadow: 0 0 8px 3px #E0E0E0;/* FF3.5+, Opera 9+, Saf1+, Chrome, IE10 */
            margin: 10px;
            -webkit-border-radius: 7px;/*Safari, Chrome*/
            -moz-border-radius: 7px;/*Firefox*/
            border-radius: 7px;
            text-decoration: none;

            }
        </style>
    </head>
    <body>
    ''')

for link in links:
    attr = link.get('onclick')
    # opening and closing on each iteration to prevent data loss
    match = re.search("^.+window.open\((\'|\")(?P<URL>[^\'\"]+)", attr)

    if match:
        m3u_list_file = codecs.open(M3U_LIST_FILE_PATH, 'a', 'utf-8')
        html_list_file = codecs.open(HTML_LIST_FILE_PATH, 'a', 'utf-8')
        popup_url = match.group('URL')

        video_url = None

        title = link.xpath('text()')[0].strip()
        print "%s [%s]" % (title, popup_url)

        if popup_url and popup_url.startswith('http'):

            popup_links.append(popup_url)
            sleep(sleeptime)

            print 'opening url %s...' % popup_url

            # guess type of link by img icon
            previous_node = link.getprevious()
            previous_node_title = previous_node.get('title')
            previous_node_alt = previous_node.get('alt')

            # SOPCAST
            if previous_node_title and 'sopcast' in previous_node_title.lower():
                print "SOPCAST link detected"

                video_url = extract_video_url_from_page(popup_url, "//param[starts-with(@value, 'sop://')]", "value")

                if (video_url):
                    m3u_list_file.write("# (SOP) %s -- %s\n" % (title, popup_url))
                    m3u_list_file.write(video_url + '\n')
                    write_link(html_list_file, video_url, "(SOP) " + title)

            # FLV PLAYER
            elif previous_node_alt and 'flvplayer' in previous_node_alt.lower():
                m3u_list_file.write("# (FLV) %s\n" % title)
                m3u_list_file.write(popup_url + '\n')
                write_link(html_list_file, popup_url, "(FLV) " + title)

            # VLC PLAYER
            elif previous_node_title and 'vlc player' in previous_node_title.lower() or not video_url:
                print "VLC link detected"

                video_url = extract_video_url_from_page(popup_url, "//embed[@type='application/x-vlc-plugin']", "target")
                if video_url:
                    m3u_list_file.write("# (VLC) %s -- %s;player=vlc\n" % (title, popup_url))
                    m3u_list_file.write(video_url + '\n')
                    write_link(html_list_file, video_url, "(VLC) " + title)



            else:
                print "WARNING: Unknown link type %s" % popup_url
        else:
            print '\tdiscarding URL', popup_url
        m3u_list_file.close()
        html_list_file.close()

with open(HTML_LIST_FILE_PATH, 'a') as f:
    f.write('''</body></html>''')

print "Done!\n"
